﻿using Acme.BookStore.DishesGroupings;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Acme.BookStore.StoreManagements
{

    [ApiExplorerSettings(GroupName = "v2")]
    public class StoreManagementServices : ApplicationService, StoreManagementIServices
    {

        IRepository<Addresses> AddressRepository;
        IRepository<StoreManagement> StoreManagement;
        private readonly IWebHostEnvironment env;

        public StoreManagementServices(IRepository<Addresses> addressRepository, IRepository<StoreManagement> StoreManagement,IWebHostEnvironment env)
        {
            AddressRepository = addressRepository;
            this.StoreManagement = StoreManagement;
            this.env = env;
        }
        
        /// <summary>
        /// 新增门店
        /// </summary> 
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddressesAdd(StoreMangementAddDTO dto)
        {
            var list = ObjectMapper.Map<StoreMangementAddDTO, StoreManagement>(dto);
            await StoreManagement.InsertAsync(list);
            return ApiResult.Ok(data: list);
        }


       
        [HttpGet("GetStoreMangementList")]
        /// <summary>
        /// 获取门店列表
        /// </summary>
        /// <param name="MerchantAddress"></param>
        /// <param name="StoreStatus"></param>
        /// <param name="StoreName"></param>
        /// <returns></returns>
        public async Task<ApiResult> GetStoreMangementList( string MerchantAddress=null, bool StoreStatus=true,string StoreName=null)
        {
            var list  =   await StoreManagement.GetListAsync(x=>x.IsDeleted==false);

            if (!string.IsNullOrEmpty(StoreName))
            { 
                list = list.Where(x => x.StoreName.Contains(StoreName)).ToList();

            }

            if (!string.IsNullOrEmpty(MerchantAddress))
            {
                list = list.Where(x => x.MerchantAddress.Contains(MerchantAddress)).ToList();
            }

            if (StoreStatus != null)
            {
                list = list.Where(x => x.StoreStatus == StoreStatus).ToList();
            }


            return ApiResult.Ok(list);

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteStoreMangement/{id}")]

        public async Task<ApiResult> DeleteStoreMangement(Guid id)
        {
         
            await StoreManagement.DeleteAsync(x=>x.Id==id);
            return ApiResult.Ok();
        }


        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("UpdateStoreMangement")]

        public async Task<ApiResult> UpdateStoreMangement(Guid id)
        {

            var list = await StoreManagement.GetListAsync(x=>x.Id==id);
            if (list.Count > 0)
            {

                if (list.First().StoreStatus==true)
                {
                    list.First().StoreStatus = false;
                }
                else
                {
                    list.First().StoreStatus = true;
                }

                
            }
            else
            {
                return ApiResult.Fail("门店不存在");

            }

            await StoreManagement.UpdateAsync(list.First());
            return ApiResult.Ok();
        }



    }
}
 